import { merge, each, isEmpty, concat, min, max, isNaN } from 'lodash'
import store from '@/store'

const cycleNum = 0
const gasRange = {
  maxGas: 3,
  maxCycle: 0.5,
  minGas: 1,
  minCycle: 0.5
}

function convertData(data) {
  const errPonts = []
  each(data, item => {
    const { value } = item
    if (value[2] > 8) {
      errPonts.push(item)
    }
  })
  return errPonts
}

function changeLevel(year = 0) {
  const l = [
    {
      min: 0,
      max: 8,
      level: 0,
      color: '#2B0FF6',
      pointMultiple: 4
    },
    {
      min: 8,
      max: 15,
      level: 1,
      color: '#CC3737',
      pointMultiple: 4
    }
  ]
  for (let i = 0; i < l.length; i++) {
    const { min, max } = l[i]
    if (Number(year) >= Number(min) && Number(year) < Number(max)) {
      return max
    }
  }
}
function bjsmfb(data, options) {
  const _serList = []

  each(data, item => {
    const { meterId, years, latitude, longitude } = item
    const year = changeLevel(years) || 0
    _serList.push({
      name: meterId,
      value: [longitude, latitude, year]
    })
  })
  return merge({}, options, {
    series: [
      {
        data: _serList
      },
      {
        data: convertData(_serList)
      }
    ]
  })
}

function bjsmrl(data, options) {
  const _serList = []
  each(data, (item, index) => {
    const { x, y, count } = item
    _serList.push([x, y, count])
  })
  return _serList
}

function bjsynxtjzf(data, options) {
  const _xAxisList = []
  const _serList = []
  each(data, item => {
    const { year, count } = item
    _xAxisList.push(year)
    _serList.push(count)
  })
  return merge({}, options, {
    xAxis: {
      categories: _xAxisList
    },
    series: [
      {
        data: _serList
      }
    ]
  })
}

function ghyhcbzqdt(data, options) {
  const _serList1 = []
  const _serList2 = []
  const { maxGas, maxCycle, minGas, minCycle } = gasRange
  each(data, item => {
    const { meterId, latitude, longitude, median, period, ...rest } = item
    if (period >= cycleNum) {
      if ((median >= maxGas && period >= maxCycle) || (median <= minGas && period <= minCycle)) {
        _serList2.push({
          name: meterId,
          value: [longitude, latitude, median],
          period,
          ...rest
        })
      } else {
        _serList1.push({
          name: meterId,
          value: [longitude, latitude, median],
          period,
          ...rest
        })
      }
    }
  })
  return merge({}, options, {
    bmap: {
      center: store.getters.mapCenter
    },
    series: [
      {
        data: _serList1
      },
      {
        data: _serList2
      }
    ]
  })
}

function bjcbzqtjzf(data, options) {
  const _xAxisList = []
  const _serList = []
  each(data, item => {
    const { max, name } = item
    _xAxisList.push(name)
    _serList.push(max)
  })
  return merge({}, options, {
    xAxis: {
      categories: _xAxisList
    },
    series: [
      {
        data: _serList
      }
    ]
  })
}

function radarSelected(data) {
  let legendData = []
  const selectedData = {}
  each(data, item => {
    if (item === 'CNG') {
      legendData = concat(item, legendData)
      selectedData[item] = true
    } else {
      legendData = concat(legendData, item)
      selectedData[item] = false
    }
  })

  return {
    legendData,
    selectedData
  }
}
function cbzqtjld(res, options) {
  const { data, maxCountByLevel } = res
  const _serList = []
  const _legendList = []
  each(data, item => {
    _serList.push({
      ...item,
      areaStyle: {
        opacity: 0.1
      }
    })
    _legendList.push(item.name)
  })
  const { legendData, selectedData } = radarSelected(_legendList)
  return merge({}, options, {
    legend: {
      data: legendData,
      selected: selectedData
    },
    radar: {
      indicator: maxCountByLevel
    },
    series: [
      {
        data: _serList
      }
    ]
  })
}

function licbzqsd(data, options) {
  const _serList = []
  each(data, item => {
    const { meterId, period, median, ...rest } = item
    if (period >= cycleNum) {
      const periodChart = [median, period]
      _serList.push({
        name: meterId,
        value: periodChart,
        ...rest
      })
    }
  })
  return merge({}, options, {
    series: [
      {
        data: _serList
      }
    ]
  })
}

function cbsjqsdt(data, options) {
  const _serList = []
  each(data, item => {
    const { meterId, deficiency, latitude, longitude } = item
    if (deficiency >= this.qsNum) {
      _serList.push({
        name: meterId,
        value: [longitude, latitude, deficiency]
      })
    }
  })
  return merge({}, options, {
    bmap: {
      center: store.getters.mapCenter
    },
    series: [
      {
        data: _serList,
        symbolSize: function(val = []) {
          const _num = isNaN(parseInt(val[2])) ? 0 : val[2]
          return (_num / 5) * 2
        }
      }
    ]
  })
}

function qbqssjtjzf(data, options) {
  const _xAxisList = []
  const _serList = []
  each(data, (item, index) => {
    const { count, minDeficiency, maxDeficiency } = item
    _xAxisList.push(index)
    _serList.push({
      name: minDeficiency + '-' + maxDeficiency,
      y: count,
      minDeficiency,
      maxDeficiency
    })
  })
  return merge({}, options, {
    xAxis: {
      categories: _xAxisList
    },
    series: [
      {
        data: _serList
      }
    ]
  })
}

function getMax(data = []) {
  const yArr = []
  each(data, item => {
    yArr.push(item.value)
  })
  const yMax = max(yArr)
  const yMin = min(yArr)
  return {
    yMax,
    yMin
  }
}
function qb30tqssjzx(data, options) {
  const _xAxisList = []
  const _serList = []
  each(data, (item, index) => {
    const { deficiency, tId } = item
    _serList.push({
      name: tId,
      value: deficiency
    })
    _xAxisList.push(Number(index) + 1)
  })
  const { yMax = 0, yMin = 0 } = getMax(_serList)
  return merge({}, options, {
    xAxis: {
      data: _xAxisList
    },
    yAxis: {
      min: yMin,
      max: yMax
    },
    series: [
      {
        data: _serList
      }
    ]
  })
}

function bjppjkfwld(data, options) {
  const _serList = []
  each(data, item => {
    const { name, value } = item
    _serList.push({
      name,
      y: value
    })
  })

  return merge({}, options, {
    series: [
      {
        data: _serList
      }
    ]
  })
}

function bjllbppbt(data, options) {
  const _serList = []
  each(data, item => {
    if (Number(item.status)) {
      _serList.push({
        name: item.status + '',
        y: item.count
      })
    }
  })

  const _vis = !isEmpty(_serList)

  return merge({}, options, {
    series: [
      {
        animation: true,
        data: _serList,
        visible: _vis
      }
    ]
  })
}

function qlrl(data, options) {
  const _serList = []
  each(data, item => {
    const { count, latitude, longitude } = item
    _serList.push([longitude, latitude, count])
  })

  return merge({}, options, {
    bmap: {
      center: store.getters.mapCenter
    },
    series: [
      {
        data: _serList
      }
    ]
  })
}

function yqlhbjslzx(data, options) {
  const _xAxisList = []
  const _firstList = []
  const _twoList = []
  each(data, item => {
    const { tId, count, sum } = item
    _xAxisList.push(tId)
    _firstList.push({
      name: tId,
      value: count
    })
    _twoList.push({
      name: tId,
      value: sum
    })
  })

  return merge({}, options, {
    xAxis: [
      {
        data: _xAxisList
      }
    ],
    series: [
      {
        data: _firstList
      },
      {
        data: _twoList
      }
    ]
  })
}

function yqlhb(data, options) {
  const _retList = []
  each(data, item => {
    const { meterGasClass, day, toDay } = item
    const _cateList = []
    const _serList = []
    _cateList.push(meterGasClass)
    _serList.push({
      y: day,
      target: toDay
    })
    _retList.push(
      merge({}, options, {
        xAxis: {
          categories: _cateList
        },
        series: [
          {
            data: _serList
          }
        ]
      })
    )
  })
  return _retList
}

function ryqlhb(data, options) {
  const _retList = []
  each(data, item => {
    const { name, count, target } = item
    const _cateList = []
    const _serList = []
    _cateList.push(name)
    _serList.push({
      y: count,
      target
    })

    _retList.push(
      merge({}, options, {
        xAxis: {
          categories: _cateList
        },
        series: [
          {
            data: _serList
          }
        ]
      })
    )
  })
  return _retList
}

function zyqlhb(data, options) {
  const _retList = []
  each(data, item => {
    const { name, count, target } = item
    const _cateList = []
    const _serList = []
    _cateList.push(name)
    _serList.push({
      y: count,
      target
    })

    _retList.push(
      merge({}, options, {
        xAxis: {
          categories: _cateList
        },
        series: [
          {
            data: _serList
          }
        ]
      })
    )
  })

  return _retList
}

function yyqlhb(data, options) {
  const _retList = []
  each(data, item => {
    const { name, count, target } = item
    const _cateList = []
    const _serList = []
    _cateList.push(name)
    _serList.push({
      y: count,
      target
    })

    _retList.push(
      merge({}, options, {
        xAxis: {
          categories: _cateList
        },
        series: [
          {
            data: _serList
          }
        ]
      })
    )
  })

  return _retList
}

const modifyData = {
  map1: bjsmfb,
  mapfw2: bjsmrl,
  bar3: bjsynxtjzf,
  map4: ghyhcbzqdt,
  bar5: bjcbzqtjzf,
  leida6: cbzqtjld,
  sandian7: licbzqsd,
  map8: cbsjqsdt,
  bar9: qbqssjtjzf,
  line10: qb30tqssjzx,
  loudou11: bjppjkfwld,
  pie12: bjllbppbt,
  map14: qlrl,
  line15: yqlhbjslzx,
  zidan16: yqlhb,
  zidan17: ryqlhb,
  zidan18: zyqlhb,
  zidan19: yyqlhb
}

export default modifyData
